function add() {
    var total = 0;
    for (let i = 0; i < arguments.length; i++) {
        total += arguments[i];
    }
    function sum() {
        //
        for (let i = 0; i < arguments.length; i++) {
            total += arguments[i];
        }
        if (arguments.length == 0) {
            return total;
        } else {
            return sum;
        }
    }
    return sum;
}
console.log(add(1)(2)(3)(4)());
console.log(add(1, 2, 3, 5)(4)());

function add(...arg) {
    var total = arg;
    function sum(...arg) {
        //      
        if (arg.length == 0) {
            return total.reduce((a, b) => a + b);
        } else {
            total = total.concat(arg);
            return sum;
        }
    }
    return sum;
}

function cd(str = "", sub) {
    let str_arr = str.split("");
    for (let i = 0; i<sub.length; i++) {
        for (let j = 0; j < str_arr.length; j++) {
            if (str_arr[j] == sub[i]) {
                str_arr.splice(j, 1);
                j--;
            }
        }
    }
    return str_arr.join("");
}
console.log(cd("They are students","aeiou"))


function bp(arr=[],index){
    let time=0;
    for(let i=0;i<arr.length;i++){       
        if(arr[i]>0){
            time++;
            arr[i]=arr[i]-1;
            if(arr[i]==0&&i==index){
                return time;
            }
        }
        if(i==arr.length-1){
            i=-1;
        }
    }
}

console.log(bp([3,2,1],0));

function pa(nums=[],k){
    let result=[];
    let index=0;
    nums.forEach(e=>{
        if(e>k){
            result.push(e);
        }else{
            index++
            result.unshift(e)
        }
    })
    return {
        result,index
    }
}
console.log(pa([1,9,3,4,768,2,14,56,41,546,3,7],30))

function findpeak(arr=[]){
    let peak=[]
    for(let i=1;i<arr.length-1;i++){
        if(arr[i-1]<arr[i]&&arr[i]>arr[i+1]){
            peak.push({
                index:i,
                value:arr[i]
            })
        }
    }
    return peak;
}
console.log(findpeak([1,2,1,3,4,5,7,6]));